//
//  MailDetailViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/25.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "MailDetailViewController.h"

@interface MailDetailViewController ()

@end

@implementation MailDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.webView.frame = CGRectMake(0, 0, kScreenW, kScreenH-20);
}
-(void)getRequestURL{
    self.user = User.currentUser;
    switch (self.type) {
        case MailTypeHospital:{
            [self loadRequestURL:^(GetURL handler) {
                self.viewModel = [[DetailViewModel alloc] init];
                NSDictionary *gidDic = @{@"user_name":self.user.username,@"user_id":self.user.userId,@"SqlNo":@"8",@"url":indicateOAMAIL_url};
                [self.viewModel getGidWithDic:gidDic Success:^(NSString *gid) {
                    NSString *requestURL = [NSString stringWithFormat:@"%@Gid=%@",HPdetail_url,gid];
                    handler(requestURL);
                }];
            }];
        }
            break;
        case MailTypeTencent:{
            [self loadRequestURL:^(GetURL handler) {
                self.viewModel = [[DetailViewModel alloc] init];
                NSDictionary *gidDic = @{@"user_name":self.user.username,@"user_id":self.user.userId,@"SqlNo":@"4",@"url":indicateQQMAIL_url};
                [self.viewModel getGidWithDic:gidDic Success:^(NSString *gid) {
                    NSString *requestURL = [NSString stringWithFormat:@"%@Gid=%@",detail_url,gid];
                    handler(requestURL);
                }];
            }];
        }
            break;
        default:
            break;
    }
}

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated || navigationAction.navigationType == WKNavigationTypeOther) {
        [super createBackBtn];
    }
    NSString *url = [[navigationAction.request URL] absoluteString];
    if ([url rangeOfString:@"LoginBind"].location != NSNotFound) {
        decisionHandler(WKNavigationActionPolicyCancel);
        [SVProgressHUD dismiss];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的账号尚未和企业邮箱关联,有任何问题请咨询网络管理部" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        //显示一秒
        [self performSelector:@selector(disAndPopView) withObject:nil afterDelay:1.5];

    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

-(void)disAndPopView{
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
